define(function(require, exports, module) {
    var ALL = require("../../common/all"),
        CONFIG = ALL.CONFIG,
        METHOD = ALL.METHOD,
        Dialog = ALL.Dialog,
        Init = ALL.Init;

    var _Dialog = new Dialog({
        title : "提示",
        content : "<p>确定是否提交？</p>",
        text4Btn: ["确定", "取消"],
        class4Btn: ["", "line"]
    });


    var _vm = Init.initVue({
        data : {
            id : null,
            res : {
                group_name : "",
                desc : "",
                group_acl : null,
            },
            authority : null
        },
        methods : {
            submit : function() {
                var item,
                    child,
                    flag,
                    url,
                    checkGroup = [];

                _vm.res.group_acl = _vm.authority;

                for (var i = 0,len = _vm.authority.length; i < len; i ++) {
                    item = _vm.res.group_acl[i];
                    item.isOwn = +item.isOwn;
                    if (item.name) delete item.name;
                    checkGroup.push({ isOwn : item.isOwn });
                    for (var j = 0, slen = item.child.length; j < slen; j ++) {
                        root = item.child[j];
                        root.isOwn = +root.isOwn;
                        if (root.name) delete root.name;
                        if (root.html) delete root.html;
                        checkGroup.push({ isOwn : root.isOwn });
                        if (root.child && root.child.length > 0) {
                            for (var k = 0, clen = root.child.length; k < clen; k ++) {
                                if (root.child[k].name) delete root.child[k].name;
                                root.child[k].isOwn = +root.child[k].isOwn;
                                checkGroup.push({ isOwn : root.child[k].isOwn });
                            }
                        }
                    }
                }

                flag = checkGroup.some(function(v){
                    if (v.isOwn) { return true; }
                });

                if (!METHOD.trim(_vm.res.group_name)) {
                    METHOD.alert("请输入角色名称");
                    return;
                } else if (!METHOD.trim(_vm.res.desc)) {
                    METHOD.alert("请输入角色说明");
                    return;
                } else if (!flag) {
                    METHOD.alert("请设置权限配置");
                    return;
                }

                if (_vm.id) {
                    url = "admin/group/update";
                    _vm.res.id = _vm.id;
                } else {
                    url = "admin/group/create";
                }

                _Dialog.showUI(function(i) {
                    if (i == 0) {
                        METHOD.loading.show();
                        METHOD.ajax(url,{ data: JSON.stringify(_vm.res)},{type : 'post'}).done(function(){
                            METHOD.loading.hide();
                            METHOD.alert("操作成功").done(function(){
                                window.location.href = "../setting/manage.html";
                            });
                        }).fail(function(){
                            METHOD.loading.hide();
                        });
                    }
                    this.hideUI();
                });
            },
            allSelect : function(n,obj) {
                var arr = obj.child;

                arr.forEach(function(v){
                    v.isOwn = n;
                    if (v.child && v.child.length > 0) {
                        v.child.forEach(function(j) {
                            j.isOwn = n;
                        });
                    }
                });
            },
            subSelect : function(n,obj,all) {
                var arr = obj.child;

                if (arr && arr.length > 0) { arr.forEach(function(v){ v.isOwn = n; }); }

                if (!all.isOwn) { all.isOwn = true; }
            },
            childSelect : function(n,sub,all,index) {
                if (!sub.isOwn) { sub.isOwn = true; }
                if (!all.isOwn) { all.isOwn = true; }
            }
        }
    });

    _vm.id = METHOD.getQueryParms("id") || null;
    if (_vm.id) {
        METHOD.loading.show();
        METHOD.ajax("admin/group/get_detail",{id : _vm.id}).done(function(json){
            METHOD.loading.hide();
            _vm.res.group_name = json.data.group_name,
            _vm.res.desc = json.data.desc,
            _vm.authority = json.data.group_acl;
        }).fail(function(){
            METHOD.loading.hide();
        });
    } else {
        METHOD.loading.show();
        METHOD.ajax("admin/group/get_base_access").done(function(json){
            METHOD.loading.hide();
            _vm.authority = json.data;
        }).fail(function(){
            METHOD.loading.hide();
        });
    }
});

